﻿// 尚未从原项目中复制过来
using System;
using System.Collections.Generic;

using System.Text;
using System.Windows.Forms;
using Husb.Util.Enum;
using System.Collections.Specialized;
using System.Data;

namespace Husb.Util
{
    public static class WinUtil
    {
        //public static void PopulateListBox(ComboBox lst, string typeName, bool showSelect)
        //{
        //    lst.Items.Clear();

        //    if (showSelect)
        //    {
        //        //lst.Items.Add(new ListItem("请选择...", "-1"));
        //        lst.Items.Add(new KeyValuePair<int, string>(-1, "请选择..."));
        //    }

        //    Dictionary<int, string> dic = EnumUtil.GetDescriptions(typeName, CategoryDataSourceType.Xml);
        //    Dictionary<int, string>.Enumerator en = dic.GetEnumerator();
        //    while (en.MoveNext())
        //    {
        //        //lst.Items.Add(new ListItem(en.Current.Value, en.Current.Key.ToString()));
        //        lst.Items.Add(new KeyValuePair<int, string>(en.Current.Key, en.Current.Value));

        //    }
        //}

        public static void PopulateListBox(ListControl lst, string typeName, bool showSelect)
        {
            Dictionary<int, string> dic = EnumUtil.GetDescriptions(typeName, CategoryDataSourceType.Xml, showSelect);

            DataTable dt = new DataTable();
            dt.Columns.Add("Key", typeof(System.Int32));
            dt.Columns.Add("Value", typeof(System.String));

            Dictionary<int, string>.Enumerator en = dic.GetEnumerator();
            while (en.MoveNext())
            {
                DataRow row = dt.NewRow();
                row["Key"] = en.Current.Key;
                row["Value"] = en.Current.Value;

                dt.Rows.Add(row);
            }

            lst.DataSource = dt;
            lst.DisplayMember = "Value";
            lst.ValueMember = "Key";
            
        }

		public static void WriteLog(string logMessage)
        {
            //string logFileName = GetLogFileName();
            string logFileName = "s.log";// GetLogFileName();
            using (StreamWriter w = File.AppendText(logFileName))
            {
                w.WriteLine(logMessage);
                w.Flush();
                w.Close();
            }
        }

        public static string GetLogFileName()
        {
            string logDirectory = @"e:\\log\"; //Application.StartupPath + LogFolder;
            if (!Directory.Exists(logDirectory))
            {
                Directory.CreateDirectory(logDirectory);
            }
            string logFile = logDirectory + DateTime.Now.ToString("yyyyMMdd") + ".txt";
            if (!File.Exists(logFile))
            {
                FileStream log = File.Create(logFile);
                log.Close();
            }
            return logFile;
        }
    }
}
